package javase.net.udp;





import java.net.DatagramPacket;
import java.net.DatagramSocket;


/**
 * 数据接收方
 * @author LiShaowen
 * @date 2020/9/5 21:23
 */
public class UdpReceiver {
    public static void main(String[] args) throws Exception {
        System.out.println("接收方启动中");
        // 1.使用DatagramSocket 指定端口创建接收端
        DatagramSocket receiver = new DatagramSocket(9999);
        // 2.准备容器 封装成DatagramPacket
        byte[] container = new byte[1024*60];

        DatagramPacket packet = new DatagramPacket(container,0,container.length);
        // 3.阻塞式接收
        receiver.receive(packet);
        // 4.分析数据
        byte[] data = packet.getData();
        int length = packet.getLength();
        System.out.println(new String(data,0,length));
        // 5.释放资源
        receiver.close();
    }
}
